package com.yang.inventorymanagement.controllerview.users;

import cn.hutool.crypto.digest.DigestUtil;

import com.yang.inventorymanagement.controllerview.HomeManage;
import com.yang.inventorymanagement.service.UserService;
import com.yang.inventorymanagement.utils.SpringContextUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;


import javax.annotation.Resource;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 用户登录传窗体
 */

@Slf4j
@Component("UserLoginSwing")
public class UserLoginSwing extends JFrame {
    /**
     * 创建窗体
     *
     * @param
     */
    @Resource
    private UserService userService;


    JPanel root;
    private JTextField usernameFiled;
    private JPasswordField passwordField;


    public UserLoginSwing() {
        setTitle("登陆界面");
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        setLocationRelativeTo(null);
        root = new JPanel();
        root.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(root);
        root.setLayout(null);


        JLabel totalLbl = new JLabel("用户登录");
        totalLbl.setFont(new Font("黑体", Font.BOLD, 20));
        totalLbl.setBounds(151, 10, 121, 28);
        root.add(totalLbl);

        JLabel usernameLbl = new JLabel("用户名");
        usernameLbl.setBounds(81, 71, 60, 15);
        root.add(usernameLbl);

        JLabel passwordLbl = new JLabel("密 码");
        passwordLbl.setBounds(81, 118, 54, 15);
        root.add(passwordLbl);

        usernameFiled = new JTextField();
        usernameFiled.setBounds(151, 68, 210, 21);
        root.add(usernameFiled);
        usernameFiled.setColumns(10);

        passwordField = new JPasswordField();
        passwordField.setBounds(151, 115, 210, 21);
        root.add(passwordField);

/*        JRadioButton radioButtonAdmin = new JRadioButton("管理员");
        radioButtonAdmin.setBounds(151, 162, 96, 23);
        //默认选中
        radioButtonAdmin.setSelected(true);


        //添加组
        ButtonGroup radioGroup = new ButtonGroup();
        radioGroup.add(radioButtonAdmin);

        root.add(radioButtonAdmin)*/
        ;


        JButton loginBtn = new JButton("登陆");
        //登陆操作
        loginBtn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                String username = usernameFiled.getText();
                String password = passwordField.getText();
                //数据校验
                if (username.equals("") || password.equals("")) {
                    System.out.println("用户名或者密码空值");
                    JOptionPane.showMessageDialog(null, "用户名密码输入错误", "提示", JOptionPane.ERROR_MESSAGE);
                    return;
                }


                //查询登录
                boolean b = userService.userLogin(username, password);
                if (b) {
                    setVisible(false);// 登陆成功，本窗口隐藏
                    //打开首页窗口
                    ((HomeManage) SpringContextUtils.getBean("HomeManage")).setVisible(true);
                    //销毁本窗口
                    dispose();
                } else {
                    JOptionPane.showMessageDialog(null, "登录失败", "message", JOptionPane.ERROR_MESSAGE);
                }


            }
        });
        loginBtn.setBounds(81, 214, 93, 23);
        root.add(loginBtn);

        JButton resetBtn = new JButton("注册");
        //注册操作
        resetBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ((UserAddSwing) SpringContextUtils.getBean("UserAddSwing")).setVisible(true);
            }
        });
        resetBtn.setBounds(268, 214, 93, 23);
        root.add(resetBtn);
    }
}
